/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idv.cloudee.proxy2.test;

import com.cirnoworks.http.utils.HTTPUtils;
import com.cirnoworks.http.utils.HeaderRequest;
import com.cirnoworks.http.utils.HeaderResponse;
import com.cirnoworks.http.utils.exception.BadRequestException;
import idv.cloudee.proxy2.framework.ProxySession;
import idv.cloudee.proxy2.impl.getpost.GETPOSTModifier;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author yuxuanhuang
 */
public class GETPOSTSessionLogger implements GETPOSTModifier, Closeable {

//    private ProxySession session;
    private static final String SESSION_NAME = "GETPOSTSessionLogger";
    private static final long startTime = System.currentTimeMillis();
    private OutputStream log;
    private boolean failed = false;

    private void println(String line) {
        if (!failed) {
            try {
                log.write(line.getBytes(HTTPUtils.ISO8859_1));
                log.write(HTTPUtils.CRLF);
            } catch (IOException e) {
                e.printStackTrace();
                failed = true;
            }
        }
    }

    public void beginRequest(ProxySession session) {
//        this.session = session;
        log = (OutputStream) session.get(SESSION_NAME);
        if (log == null) {
            String path = "logs/session/" + startTime + "/";
            File f = new File(path);
            f.mkdirs();
            try {
                log = new FileOutputStream(path + session.getSessionId() + ".log");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(GETPOSTSessionLogger.class.getName()).log(Level.SEVERE, null, ex);
            }
            session.put(SESSION_NAME, log);
            session.addCloseable(this);
        }

    }

    public HeaderRequest modifyRequest(HeaderRequest param) throws BadRequestException {
        println("=======RequsstHead======");
        println(param.getRequestLine());
        println(param.toString());
        println("");
        return param;
    }

    public OutputStream getPOSTOutputStream(OutputStream os) {
        println("=======RequsstData======");
        return new CopyOutputStream(os, log, false);
    }

    public HeaderResponse modifyResponse(HeaderResponse param) {
        println("=======ResponseHead=====");
        println(param.getResponseLine());
        println(param.toString());
        return param;
    }

    public OutputStream getResponseOutputStream(OutputStream os) {
        println("=======ResponseData=====");
        return new CopyOutputStream(os, log, false);
    }

    public void requestOver() {
        println("=======Over=====");
    }

    public void close() throws IOException {
        println("=======SessionOver=====");
        try {
            log.close();
        } catch (IOException e) {
        }
    }
}
